/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Views;

import dao.ClienteDAO;
import dao.ContaDAO;
import entity.Cliente;
import entity.Conta;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Paulinha
 */
public class ListarCliente extends javax.swing.JFrame{
    private  int tipoconta;
    /**
     * Creates new form ListarCliente
     */
//    private ResultSetTableModel dataModel;
//    private TableModel dataModel;
//    private ResultSetTableModel model;
    private ModeloTabelaCliente modelo;
    public ListarCliente() {
          initComponents();
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jNome = new javax.swing.JTextField();
        jSobrenome = new javax.swing.JTextField();
        jRg = new javax.swing.JTextField();
        jCpf = new javax.swing.JTextField();
        ButtonBuscar = new javax.swing.JButton();
        ButtonVoltar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        try{
            jTable2 = new javax.swing.JTable();
            ButtonAlterar = new javax.swing.JButton();
            ButtonExcluir = new javax.swing.JButton();
            ButtonVincular = new javax.swing.JButton();
            ButtonManipular = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
            jLabel1.setText("Listar Cliente");

            jLabel2.setText("Nome");

            jLabel3.setText("Sobrenome");

            jLabel4.setText("RG");

            jLabel5.setText("CPF");

            jNome.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jNomeActionPerformed(evt);
                }
            });

            ButtonBuscar.setText("Listar");
            ButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ButtonBuscarActionPerformed(evt);
                }
            });

            ButtonVoltar.setText("Voltar");
            ButtonVoltar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ButtonVoltarActionPerformed(evt);
                }
            });
            ButtonVoltar.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    ButtonVoltarKeyPressed(evt);
                }
            });

            ClienteDAO dao2 = new ClienteDAO();
            //dataModel = new TableModel(dao2.listar());
            modelo = new ModeloTabelaCliente(dao2.listar());
            jTable2.setModel(modelo);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        jScrollPane2.setViewportView(jTable2);

        ButtonAlterar.setText("Alterar Cliente");
        ButtonAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAlterarActionPerformed(evt);
            }
        });

        ButtonExcluir.setText("Excluir Cliente");
        ButtonExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonExcluirActionPerformed(evt);
            }
        });

        ButtonVincular.setText("Vincular à Conta");
        ButtonVincular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonVincularActionPerformed(evt);
            }
        });

        ButtonManipular.setText("Manipular Conta");
        ButtonManipular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonManipularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addComponent(ButtonManipular, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonVincular)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonExcluir)
                .addGap(32, 32, 32))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(64, 64, 64)
                                .addComponent(jNome))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addGap(72, 72, 72)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRg, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                                    .addComponent(jSobrenome, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jCpf)))
                            .addComponent(jLabel3))
                        .addGap(38, 38, 38))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                        .addGap(17, 17, 17)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ButtonBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ButtonVoltar, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addGap(14, 14, 14))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jSobrenome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonVoltar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonAlterar)
                    .addComponent(ButtonExcluir)
                    .addComponent(ButtonVincular)
                    .addComponent(ButtonManipular))
                .addGap(15, 15, 15))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonVoltarActionPerformed
        TelaCliente tela = new TelaCliente();
        tela.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_ButtonVoltarActionPerformed

    private void ButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonBuscarActionPerformed
        Cliente cliente = new Cliente();
        cliente.setNome(jNome.getText());
        cliente.setSobrenome(jSobrenome.getText());
        cliente.setRg(jRg.getText());
        cliente.setCpf(jCpf.getText());
        ClienteDAO dao = new ClienteDAO();        
        try {       
//            dataModel = new TableModel(dao.consultarCliente(cliente));
            modelo = new ModeloTabelaCliente(dao.consultarCliente(cliente));
//        } catch (SQLException ex) {
          } catch (Exception ex) {
            Logger.getLogger(ListarCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
//        jTable2.setModel(dataModel);
        jTable2.setModel(modelo);
    }//GEN-LAST:event_ButtonBuscarActionPerformed

    private void ButtonVoltarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ButtonVoltarKeyPressed
       this.ButtonVoltarActionPerformed(null);
    }//GEN-LAST:event_ButtonVoltarKeyPressed

    private void ButtonAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAlterarActionPerformed
        Cliente cliente = new Cliente();
        int row = jTable2.getSelectedRow();
        cliente = modelo.getCliente(row);
        if(cliente == null){
            JOptionPane.showMessageDialog(null, "selecione um cliente");
        }
        else{
        AtualizarCliente tela = new AtualizarCliente(cliente);
        tela.setVisible(true);
        this.setVisible(false);
        }
    }//GEN-LAST:event_ButtonAlterarActionPerformed

    private void ButtonVincularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonVincularActionPerformed
        try {           
            Cliente cliente = new Cliente();
            ClienteDAO dao = new ClienteDAO();
            int row = jTable2.getSelectedRow();                                     
            if(row != -1){
                cliente = modelo.getCliente(row);  
                tipoconta = dao.verificainvestimento(cliente.getIdCliente());
                if(tipoconta == 0){
                    VincularCliente tela;
                    tela = new VincularCliente(cliente);
                    tela.setVisible(true);
                    this.setVisible(false);
                }
                else{
                    JOptionPane.showMessageDialog(null, "Este Cliente já contem uma conta");
                }
            }else{
               JOptionPane.showMessageDialog(null, "Selecione um Cliente"); 
            }                
        } catch (SQLException ex ) {
            Logger.getLogger(ListarCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ButtonVincularActionPerformed

    private void ButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonExcluirActionPerformed
        Cliente cliente = new Cliente();
        int row = jTable2.getSelectedRow();
        cliente = modelo.getCliente(row);
        ExcluirCliente tela = new ExcluirCliente(cliente);
        tela.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_ButtonExcluirActionPerformed

    private void jNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jNomeActionPerformed

    private void ButtonManipularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonManipularActionPerformed
        try {
            Cliente cliente = new Cliente();
            Conta conta = new Conta() {};
            ContaDAO contadao = new ContaDAO();
            ClienteDAO dao = new ClienteDAO();
            int row = jTable2.getSelectedRow();
            cliente = modelo.getCliente(row);
            tipoconta = dao.verificainvestimento(cliente.getIdCliente());
            if(row != -1){
                if(tipoconta == 0){
                    JOptionPane.showMessageDialog(null, "Este Cliente não contem uma conta");
                }else{
                    conta = contadao.getConta(cliente.getIdCliente(), tipoconta);
                    ManipulaOpcoes tela = new ManipulaOpcoes(cliente, tipoconta, conta);
                    tela.setVisible(true);
                    this.setVisible(false);
                }         
            }else{
                JOptionPane.showMessageDialog(null, "Selecione um Cliente");
            }
        } catch (SQLException ex) {
            Logger.getLogger(ListarCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ButtonManipularActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               
                    new ListarCliente().setVisible(true);
               
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonAlterar;
    private javax.swing.JButton ButtonBuscar;
    private javax.swing.JButton ButtonExcluir;
    private javax.swing.JButton ButtonManipular;
    private javax.swing.JButton ButtonVincular;
    private javax.swing.JButton ButtonVoltar;
    private javax.swing.JTextField jCpf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jNome;
    private javax.swing.JTextField jRg;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jSobrenome;
    private static javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

}
